#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
require Exporter;
use CGI qw(param);
use SIM_settings;
use SIM_base;
use strict;


# Startupscript casetool, part of Sipman.

my $tool = $ARGV[0];
my $action = $ARGV[1];
my $option1 = $ARGV[2];
my $option2 = $ARGV[3];
my $option3 = $ARGV[4];


my $backHeader="black";
my $httpUser = $ENV{'REMOTE_USER'};
my $httpReF = $ENV{'HTTP_REFERER'};
my $httpScriptName = $ENV{'SCRIPT_NAME'};
my $httpUserIp = $ENV{'REMOTE_ADDR'};
my $httpServerName = $ENV{'HTTP_HOST'};
my $httpCookie = $ENV{'HTTP_COOKIE'};
my $httpHost = $ENV{'HTTP_HOST'};
my $domainDir = $ENV{'SIM_DOMAINDIR'};

my $helpDir = "$SIMparam{'SIMhelpDir'}/$SIMparam{'SIMhelpLanguage'}";
# variable to temporary store 
my @head = ();
my @index = ();
my @data = ();


sub printMainMenu {
  my $fontColor = "#222222";
  print("<FONT COLOR=black>Documentation: </FONT>");
  # show the documentation for Sipman (and allow to update on the devellopment stations)
  open ( DOC, "$SIMparam{'SIMdocumentation'}" ) || die "Oops, could not open SIMdocumentation file.";
  my @info = <DOC>;
  close DOC;
  my $first = "";
  foreach my $entry (@info) {
    my ( $menuEntry, $description ) = split(' ', $entry, 2);
    if ( "$menuEntry" ne "#" ) {
      if ( "$first" ne "" ) {
        print(" <FONT COLOR=white>|</FONT> ");
      }
      $first = $menuEntry;
      print("<A HREF=$httpScriptName?$tool+display+file+manual+$menuEntry><FONT COLOR=$fontColor>$menuEntry</FONT></A>");
    }
  }
  # if there is an writeable helppage, offer to update is in the menu
  my $fileName = "$helpDir/$option2+$option3";
  if ( -w $fileName ) {
    print("
      <FONT COLOR=black>Action:</FONT>
      <A HREF=$httpScriptName?$tool+updatePage+file+$option2+$option3><FONT COLOR=$fontColor>Update</FONT></A>
    ");
  }
  print("</TD></TR></TABLE><CENTER>");
}

sub displayHelpInfo {
  my $subTool = "@_[0]";
  my $action = "@_[1]";
# print("Info for $tool, $action");
  my $fileName = "$helpDir/$subTool+$action";
  if ( -f $fileName ) {
    print("<TABLE><TR><TD>");
    open ( FILE, $fileName );
    my @lines = <FILE>;
    close FILE;
    foreach my $entry (@lines) {
      chomp $entry;
      print("$entry");
    }
    print("</TD></TR></TABLE>");
  } else {
    if ( ! -w $helpDir ) {
      print("<H3>Sorry no help info found for $subTool+$action</H3>\n");
    } else {
      print("<H3>No helpinfo found for $tool+$action.<BR>
        Please <A HREF=$httpScriptName?$tool+addPage+file+$subTool+$action><B>add</B></A> the help page.</H3>\n");
    }
  }
}

sub addPage {
  my $subTool = "@_[0]";
  my $action = "@_[1]";
  my $title = param("title");
  if ( ! -w $helpDir ) {
    print("<H3>oops, cannot write to the helpdir.</H3>");
  } elsif ( "$title" eq "" ) {
    print("<H3>Title for help-page: $subTool+$action</H3>
           Please add the title displayed at the top of the help-page.
           <FORM method=post Action=$httpScriptName?$tool+addPage+file+$subTool+$action>
             <INPUT NAME=title Size=80 TYPE=text>
             <INPUT TYPE=submit Value=Update>
	   </FORM>");
  } else {
    my $fileName = "$helpDir/$subTool+$action";
    print("<H2>Setting title to: $title</H2>");
    printFileLine( $fileName, "title for $subTool+$action", "<CENTER><H2>\n$title\n</H2></CENTER>\n");
    updSection( "bottom", $subTool, $action );
  }
}

sub updSection {
  my $section = "@_[0]";
  my $subTool = "@_[1]";
  my $action = "@_[2]";
  my $title = "@_[3]";
  my $text = "@_[4]";
  my $formData = param($section);
  if ( "$formData" eq "" ) {
    $text =~ s/<BR>/\n/g;
    $text =~ s/^\s//g;
    print("<H3>Add paragraph</H3>
      <FORM method=post Action=$httpScriptName?$tool+updatePage+$section+$subTool+$action>
        Title: <INPUT NAME=\"title\" TYPE=text VALUE=\"$title\" SIZE=150>
        <TEXTAREA NAME=\"$section\" ROWS=20 COLS=150>$text</TEXTAREA><BR>
        <INPUT TYPE=submit Value=Update>
      </FORM>");
  } else {
    $title = param("title");
    $formData =~ s/\r\n/<BR>/g;
    $formData =~ s/<BR>$//g;
    $text = $formData;
    if ( $section eq "bottom" ) {
      $section = time;
    }
  }
  if ( "$text" ne "" ) {
    my $info = "<!--- TITLE $section ---><A NAME=$section></A><B>\n$title\n<!--- DATASTART $section ---></B><BR>\n$text\n<!--- DATAEND $section ----><HR>\n";
    push( @data, $info );
    print( $info );
  }
  if ( "$title" ne "" ) {
    push(@index, "<LI><A HREF=#$section>$title</A>");
  }
}

sub printSectionMenu {
  my $section = "@_[0]";
  my $subTool = "@_[1]";
  my $action = "@_[2]";
  print("</TD></TR><TR><TD ALIGN=center BGCOLOR=#CC88CC>
            <A HREF=$httpScriptName?$tool+updatePage+$section+$subTool+$action>update</A>
	 </TD></TR><TR><TD>");
}

sub updatePage {
  my $section = "@_[0]";
  my $subTool = "@_[1]";
  my $action = "@_[2]";
# my @head = ();
# my @index = ();
  my $fileName = "$helpDir/$subTool+$action";
  open( IN, $fileName ) || die "oops, updatePage: could not open helpfile for reading.\n";
  my @info = <IN>;
  close IN;
  my $state = "head";
  my $sectionText;
  my $sectionTitle;
  my $num = 1;
  print("<TABLE><TR><TD>");
  foreach my $entry (@info) {
    chomp $entry;
    # skip the index, will be build later.
    if ( $entry eq "<!--- INDEXEND --->" ) {
      $state = "";
    } elsif ( $entry eq "<!--- INDEX --->" ) {
      $state = "index";
    } elsif ( substr($entry, 0, 11 ) eq "<!--- TITLE" ) {
      $state = "title";
#     print("A NAME ($numInfo)<BR>");
    } elsif ( substr($entry, 0, 15 ) eq "<!--- DATASTART" ) {
      $state = "data";
    } elsif ( substr($entry, 0, 13 ) eq "<!--- DATAEND" ) {
      my ( $dummy1, $dummy2, $numInfo, $rest ) = split(' ', $entry);
      if ( $numInfo eq $section ) {
        updSection( $numInfo, $subTool, $action, $sectionTitle, $sectionText );
      } else {
        # menu line with options
#	print("<HR>");
	printSectionMenu( $numInfo,$subTool,$action );
        push(@index, "<LI><A HREF=#$numInfo>$sectionTitle</A>");
	$sectionText =~ s/<BR>$//g;
	my $sectionData = "<!--- TITLE $numInfo ---><A NAME=$numInfo></A><B><FONT SIZE=+1>\n$sectionTitle\n<!--- DATASTART $numInfo ---></FONT></B><BR>\n$sectionText\n<!--- DATAEND $numInfo ----><HR>\n";
        push( @data, "$sectionData");
	print("$sectionData");
        $num++;
      }
      $sectionTitle = "";
      $sectionText = "";
    } elsif ( $state eq "title" ) {
      $sectionTitle = $entry;
      $state = "";
    } elsif ( $state eq "data" ) {
      $sectionText = "$sectionText $entry<BR>";
    } elsif ( "$state" eq "head" ) {
      push( @head, "$entry\n" );
      print("$entry");
    }
  }
  if ( "$section" eq "bottom" ) {
    updSection( $section, $subTool, $action );
  } 
  printSectionMenu( "bottom",$subTool,$action );
  open( OUT, ">$fileName" ) || die "oops, updatePage: could not open helpfile for writing.\n";
  foreach my $entry (@head) {
    print OUT $entry;
  }
  print OUT ("<!--- INDEX --->\n<HR><H3>Index:</H3><OL>");
  foreach my $entry (@index) {
    print OUT $entry;
  }
  print OUT ("</OL><P>\n<!--- INDEXEND ---><HR>\n");
  foreach my $entry (@data) {
    print OUT $entry;
  }
  close OUT;
  print("</TD></TR></TABLE>");
}

# ------------------------------------- Main ---------------------------------------

&printMainMenu;
#print("<CENTER>");
if ( "$action" eq "display" ) {
  displayHelpInfo($option2, $option3 );
} elsif ( "$action" eq "addPage" ) {
  addPage($option2, $option3 );
} elsif ( "$action" eq "updatePage" ) {
  updatePage($option1, $option2, $option3 );
} else {
  print("Oops, donot know what to do ($action)");
}
print("</TABLE>");
#&printFooter;
